/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-24
 * Time: 13:55
 */

import java.io.*;
import java.util.Scanner;

/**
 * 给一个目录,判断其文件中是否包含某个关键字
 *
 */
public class Demo3 {
    public static void main(String[] args) throws IOException {
        System.out.println("请输入一个目录");
        Scanner scanner = new Scanner(System.in);
        String rootPath = scanner.next();
        System.out.println("请输入关键字");
        String searchWord = scanner.next();
        File f =new File(rootPath);
        if(!f.isDirectory()){
            System.out.println("路径错误");
            return;
        }
        searchWord(f,searchWord);
    }
    public static void searchWord(File f , String searchWord) throws IOException {
        File[] files =f.listFiles();
        if(files==null){
            return;
        }
        for (  File file : files ) {
            if(file.isFile()){
                String s =file.getName();
                if(s.contains(searchWord)){
                    System.out.println(file.getAbsolutePath());

                }
                try(Reader reader = new FileReader(file)){
                    while(true){
                        int n= reader.read();
                        if(n==-1){
                            break;
                        }
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append((char)n);
                        if(stringBuilder.indexOf(searchWord)>=0){
                            System.out.println(file.getAbsolutePath());
                            break;
                        }
                    }


                }
            }else{
                searchWord(file,searchWord);
            }
        }
    }
}
